package com.focusai.efairy.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.focusai.efairy.R;
import com.focusai.efairy.model.report.AlarmScaleMapEntity;
import com.focusai.efairy.model.report.AnalyseDataType;
import com.focusai.efairy.model.report.ReportNumberInfo;
import com.focusai.efairy.model.report.WarnningDataType;
import com.focusai.efairy.model.request.GetReportDataRequest;
import com.focusai.efairy.model.response.ReportDataResponse;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.activity.report.ReportActivity;
import com.focusai.efairy.ui.activity.report.ReportFilterBaseFragment;
import com.focusai.efairy.ui.adapter.report.ReportAnalyseDevViewBinder;
import com.focusai.efairy.ui.adapter.report.ReportArcViewBinder;
import com.focusai.efairy.ui.adapter.report.ReportInfoViewBinder;
import com.focusai.efairy.ui.adapter.report.ReportWarnningViewBinder;
import com.focusai.efairy.ui.adapter.report.ReportWavaAnalyseViewBinder;
import com.focusai.efairy.utils.DateUtils;
import com.focusai.efairy.utils.report.ColorTemplate;
import com.focusai.efairy.utils.report.ReportUtils;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by luo_guangwen on 2017/8/1.
 */

public class ReportFragment extends ReportFilterBaseFragment {
    private final int EVENT_DATA = 0X11;
    private RecyclerView mRecyclerView;
    private MultiTypeAdapter mAdapter;
    private List<Object> items;
    private String projectId;
    private String startDate,endDate;
    private ReportNumberInfo reportNumberInfo; //页面最后那个报告
//    private int dateType; // 1-日报 2-周报 3-月报 4-季报 5-年报，传区间内的一个时间
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_report, container, false);
        initView(view);
        bindEvents();
        initData();
        setDefaultValues();
        return view;
    }

    @Override
    protected boolean useHandleThread() {
        return true;
    }

    @Override
    protected void initView(View view) {
        mRecyclerView = findView(view, R.id.rcyc_list);
        super.initView(view);
    }

    @Override
    protected void setDefaultValues() {
        if (getArguments() != null){
            projectId = getArguments().getString(ReportActivity.KEY_PROJECT_ITEM_ID);
        }
        mAdapter = new MultiTypeAdapter();
        int[] colors = getContext().getResources().getIntArray(R.array.pie_colors);
        mAdapter.register(AnalyseDataType.class, new ReportWavaAnalyseViewBinder(getContext()));
        mAdapter.register(AnalyseDataType.OverviewAnalyze.class, new ReportAnalyseDevViewBinder(getContext()));
        mAdapter.register(AlarmScaleMapEntity.class, new ReportArcViewBinder(ColorTemplate.PIE_COLORS, getContext()));
        mAdapter.register(WarnningDataType.class, new ReportWarnningViewBinder(colors, getContext()));
        mAdapter.register(ReportNumberInfo.class, new ReportInfoViewBinder(getContext()));
        items = new ArrayList<>();
        mAdapter.setItems(items);
        mRecyclerView.setAdapter(mAdapter);
        reportNumberInfo = new ReportNumberInfo();
        loadData();
        super.setDefaultValues();
    }

    @Override
    protected boolean procHandlerCallback(Message msg) {
        switch (msg.what){
            case EVENT_DATA:
                ReportDataResponse data = (ReportDataResponse) msg.obj;
                if (data != null){
                    WarnningDataType warnningDataType = ReportUtils.getWarnningDatas(data.alarm_data_list,
                            DateUtils.parseDefaultDate(startDate).getTime(),
                            DateUtils.parseDefaultDate(endDate).getTime(),
                            getCurrDateType());
                    items.clear();
                    items.add(data.overview);
                    items.addAll(data.overview.overview_analyze_list);
                    items.addAll(data.alarm_scale_map_list);
                    items.add(warnningDataType);
                    reportNumberInfo.setWeiDianNum(data.getDeviceInstallNum());
                    reportNumberInfo.setWarningNum(data.getDeviceAlarmNum());
                    int normalNum = reportNumberInfo.getWeiDianNum() - reportNumberInfo.getWarningNum();
                    reportNumberInfo.setNormalNum(normalNum);
                    reportNumberInfo.setTotalWarningNum(data.getDeviceTotalWarningNum());
                    reportNumberInfo.setData(txtCurrentDay.getText().toString());
                    items.add(reportNumberInfo);
                    getUiHandler().sendEmptyMessage(EVENT_DATA);
                }
                break;
            default:
                break;
        }
        return super.procHandlerCallback(msg);
    }

    @Override
    protected boolean uiHandlerCallback(Message msg) {
        switch (msg.what){
            case EVENT_DATA:
                mAdapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
        return super.uiHandlerCallback(msg);
    }

    public void loadData(){
        showSingleDialog("正在加载数据..");
        startDate = getStartDateStr();
        endDate = getEndDateStr();
        HttpManager.getInstance().sendRequest(new GetReportDataRequest(projectId,startDate, endDate, new Response.Listener<ReportDataResponse>() {
            @Override
            protected void onSuccess(ReportDataResponse response) {
                closeSingleDialog();
                if (response != null && response.alarm_data_list != null && response.alarm_scale_map_list != null){
                    Message msg = getProcHandler().obtainMessage();
                    msg.what = EVENT_DATA;
                    msg.obj = response;
                    getProcHandler().sendMessage(msg);
                }
            }

            @Override
            protected void onFail(NetworkException exception) {
                closeSingleDialog();
                doException(exception);
            }
        }));
    }
}
